//
//  WCWebExtractItem.m
//  WebClipper
//
//  Created by Daniel Braun on 25/02/07.
//  Copyright 2007 Daniel Braun http://page-appart.net, http://braun.daniel.free.fr All rights reserved.
//

#import "WCconfig.h"
#import "WCWebExtractItem.h"
#import "WCWebExtractionPath.h"
#import "WCExtractAction.h"
#import "StringExtension.h"
#ifndef NODOM
#import <WebKit/DOM.h>
#else
#import "WCDom.h"
#endif

@implementation WCWebExtractItem
- (id) init {
	self = [super init];
	if (self != nil) {
		name=@"item";
	}
	return self;
}
- (void) dealloc {
	[name release];
	[extractAction release];
	[xpath release];

	[super dealloc];
}

- (NSString *) description
{
	return [NSString stringWithFormat:@"WCWebExtractItem %@ action \"%@\" path %@",
		name, extractAction, xpath];
}

- (id) initWithCoder:(NSCoder *) coder
{
	//self=[super initWithCoder:coder];
	self=[super init];
	if ([coder allowsKeyedCoding]) {
		name=[[coder decodeObjectForKey:@"name"]retain];
		xpath=[[coder decodeObjectForKey:@"xpath"]retain];
		extractAction=[[coder decodeObjectForKey:@"extractAction"] retain];
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	//[super encodeWithCoder:coder];
	if ( [coder allowsKeyedCoding] ) {
		[coder encodeObject:name forKey:@"name"];
		[coder encodeObject:xpath forKey:@"xpath"];
		[coder encodeObject:extractAction forKey:@"extractAction"];
	}
}

- (NSString *) name
{
	return name;
}

- (NSString *) jsCodeForXPathArgs:(NSArray *)arg
{
	return [xpath jsCode];
}

- (NSString *) jsCode
{
	NSString *z=[[NSString stringFromResourceFile:@"js.2"] 
		parseWithActions:[NSDictionary dictionaryWithObjectsAndKeys:
						      @"jsCodeForXPathArgs:", @"xpath",
			nil]
			     for:self andSimpleReplacement:[NSDictionary dictionaryWithObjectsAndKeys:
				     name ? name : @"unnamed", @"name",
				     nil]];
	
	return z;
}

- (WCWebExtractionPath *) xpath
{
	if (!xpath) {
		xpath=[[WCWebExtractionPath alloc]init];
	}
	return xpath;
}

- (NSDictionary *) processExtractionWithDom:(DOMHTMLElement *)rootElement skipAction:(BOOL)skip fromUrl:(NSURL *)url
{
	DOMHTMLElement *el=[xpath extractFrom:rootElement];
	if (!el) {
		NSLog(@"extraction failed for %@ (%p)\n", self, self);
		//try again, for debug
		el=[xpath extractFrom:rootElement];
	}
	//NSLog(@"jhop el=%@\n", el);
	id value=nil;
	
	if (!skip && extractAction) {
		value=[extractAction handle:el fromUrl:url];
	}
	if (!value) {
		value=@"?";
		if ([el respondsToSelector:@selector(outerHTML)]) value=[el outerHTML];
		else if ([el respondsToSelector:@selector(nodeValue)]) {
			value=[el nodeValue];
		} else if ([el respondsToSelector:@selector(stringValue)]) value=[(id)el stringValue];
		else {
			NSLog(@"no value for node %@ / name %@\n", el, name);
		}
	}
	NSDictionary *d=[NSDictionary dictionaryWithObject:value forKey:name];
	//NSLog(@"extracted %d items\n", [d count]);
	return d;
}

- (WCExtractAction *) extractAction
{
	return extractAction;
}

- (void) setExtractAction:(WCExtractAction *)a
{
	if (a==extractAction) return;
	[extractAction release];
	extractAction=[a retain];
}


@end
